package com.mhys.hms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhys.hms.entity.FeedBackObject;
import com.mhys.hms.entity.User;
import com.mhys.hms.service.user.RecrManagerService;
import com.mhys.hms.service.user.UserService;

@Controller
public class UserManager {
	@Autowired
	private UserService userService;
	@Autowired
	private RecrManagerService recrManagerService;

	@RequestMapping("/addUserInfo")
	@ResponseBody
	public FeedBackObject addUserInfo(String id, String name, String pwd, String empId, String type, String realName) {
		FeedBackObject fbo = new FeedBackObject();

		// 判断添加的账户信息是否存在
		List<User> list = userService.queryUserByCount(name);
		if (list.size() > 0) {
			fbo.success = "FAIL";
			fbo.msg = "用户名存在。";
		} else {
			// 将参数设置进实体类
			User user = new User();
			if (empId == null || "".equals(empId)) {
				user.setEmpId(null);
			} else {
				user.setEmpId(Integer.parseInt(empId));
			}
			user.setName(name);
			user.setPwd(pwd);
			user.setRealName(realName);
			user.setType(type);

			// 添加新账号
			int row = userService.addUser(user);
			if (row > 0) {
				fbo.success = "OK";
				recrManagerService.deleteRecrById(Integer.parseInt(id));
			}

		}

		return fbo;
	}

	@RequestMapping("/queryUserInfo")
	@ResponseBody
	public FeedBackObject queryUserInfo(String userNo, String userName, int pageNumber, int pageSize) {
		FeedBackObject fbo = new FeedBackObject();
		List<User> list = userService.getUserInfo(pageNumber, pageSize, userNo, userName);
//		if(list.size()>0) {
//			fbo.success = "OK";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		map.put("total", userService.getUserCount(userNo, userName));
		fbo.setObj(map);
//		}
		return fbo;
	}
	
	@RequestMapping("/delUserInfo")
	@ResponseBody
	public FeedBackObject delUserInfo(String id) {
		FeedBackObject fbo = new FeedBackObject();
		int row = userService.delUserById(id);
		if(row > 0) {
			fbo.success = "OK";
		}
		return fbo;
	}
}
